package com.easy.base.api.advice;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.easy.base.api.model.dto.ResultBody;
import com.easy.base.api.model.enums.ShowType;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {
    @Resource
    private HttpServletRequest request;

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ResponseEntity<ResultBody<String>> exceptionHandler(Exception e) {
        log.error(e.getMessage());
        ResultBody<String> body = new ResultBody<>();
        body.setSuccess(true);
        body.setErrorCode("500");
        body.setErrorMessage(e.getMessage());
        body.setShowType(ShowType.MessageError);
        return ResponseEntity.internalServerError().body(body);
    }
}
